<?php

class ProductController extends Controller {

    /**
     *
     * @param type $id 
     */
    public function actionIndex($id = null) {
        $br = array();
        if ($id != null ) {
            $product = Product::model()->findByPk($id);
            if ($product != null) {
                //
                $descendants = $product->category->ancestors()->findAll();
                foreach($descendants as $item){
                   if($item->id != 1){
                       $this->breadcrumbs[] = array(
                            'title' => $item->name,
                            'link' => $item->getUrl()
                        ); 
                   }
                }
                $this->breadcrumbs[] = array(
                        'title' => $product->category->name,
                        'link' => $product->category->getUrl(),
                );
                $this->breadcrumbs[] = array(
                        'title' => $product->name,
                        'link' => '',
                );
                
                //seo
                $this->pageTitle = $product->getSeoTitle();
                $product->registerMeta();
                $description = $product->seo_description == null || strlen($product->seo_description) == 0?$product->name:$product->seo_description;
                $this->facebook = '<meta property="og:title" content="'.$product->name.'" />
                <meta property="og:type" content="product" />
                <meta property="og:url" content="http://'.$_SERVER['SERVER_NAME'].$product->getUrl().'" />
                <meta property="og:image" content="http://'.$_SERVER['SERVER_NAME'].urlImageFolder('product').$product->getImage().'" />
                <meta property="og:description" content="'.$description.'" />
                <meta property="og:site_name" content="7buy" />';
                
                //
                $view = $product->view;
                $view++;
                Product::model()->updateAll(array('view' => $view), 'product_id = :product_id', array(':product_id' => $id));
                
            } else {
                throw new CHttpException(404, 'Sản phẩm không được tìm thấy.');
            }
        } else {
            throw new CHttpException(404, 'Sản phẩm không được tìm thấy');
        }
        $this->render('index', array('product' => $product,'br' => $br));
    }


}